package com.gxa.service.impl;

import com.gxa.common.ResponseData;
import com.gxa.common.Result;
import com.gxa.mapper.SysUserMapper;
import com.gxa.pojo.Sys_User;
import com.gxa.service.SysUserService;
import com.gxa.utils.JwtUtils;
import com.gxa.utils.MD5Utils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public ResponseData login(String username, String password) {

        ResponseData responseData = new ResponseData();
        //加密
        String newpwd = MD5Utils.encrypt(password,MD5Utils.MD5_KEY);
        System.out.println(newpwd);
        Sys_User sys_user = sysUserMapper.findSysUserByUsername(username);
        if (sys_user != null && sys_user.getPassword().equals(newpwd)){
  //登录成功
        responseData.setCode(200);
        responseData.setMsg("登陆成功");
        String token = JwtUtils.createJwt(sys_user.getId().toString(),JwtUtils.JWT_SECERT,username,JwtUtils.TTMILLIS);
        responseData.setToken(token);
    }else {
        //登录失败
        responseData.setCode(500);
        responseData.setMsg("账户或密码错误");
    }
        return responseData;
    }

    @Override
    public Result findSysUserList(Integer currentPage, Integer pageSize) {
        Result r = new Result();
        Integer param1 = (currentPage - 1)*pageSize;
        Integer param2 = pageSize;
        List<Sys_User> sysUserList = sysUserMapper.findSysUserList(param1, param2);
        r.setCode(200);
        r.setMsg("success");
        r.setData(sysUserList);
        return r;
    }
}










